package leetcode.hash;

import java.util.HashSet;
import java.util.Set;

public class Test202 {
    public static void main(String[] args) {
        boolean happy = isHappy(19);
        System.out.println(happy);
    }
    public static boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        int temp = n;
        int sum = 0;
        while (true) {
            while (temp > 0) {
                int i = temp % 10;
                sum += i * i;
                temp = temp/10;
            }
            if (sum == 1) {
                return true;
            }
            if (!set.add(sum)) {
                return false;
            }
            temp  = sum;
            sum = 0;
        }
    }
}
